define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/index',
                    add_url: 'user/user/add',
                    edit_url: 'user/user/edit',
                    del_url: 'user/user/del',
                    multi_url: 'user/user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                pageSize: 10,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'last_login_time', title: __('Logintime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'add_time', title: __('Jointime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'login_source', title: __('来源'), formatter: Table.api.formatter.status, searchList: {'dy': '抖音', 'xcx': '微信小程序'}},
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {'1': '正常', '0': '冻结','-1':'注销'}},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                            ]
                        }
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        balance: function () {
            Controller.api.bindevent();
        },
        finance: function () {
             // 初始化表格参数配置
             Table.api.init({
                extend: {
                    index_url: 'user/user/finance?ids='+ Config.ids,
                    table: 'user',
                }
            });
            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'title', title: __('说明'), operate: 'LIKE'},
                        {field: 'rose_money', title: __('积分变动'), operate: 'LIKE'},
                        {field: 'add_time', title: __('Jointime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        address: function () {
            // 初始化表格参数配置
            Table.api.init({
               extend: {
                   index_url: 'user/user/address?ids='+ Config.ids,
                   table: 'user',
               }
           });
           var table = $("#table");
           // 初始化表格
           table.bootstrapTable({
               url: $.fn.bootstrapTable.defaults.extend.index_url,
               pk: 'id',
               sortName: 'id',
               columns: [
                   [
                       {field: 'id', title: __('Id'), sortable: true},
                       {field: 'name', title: __('收货人姓名'), operate: 'LIKE'},
                       {field: 'address', title: __('收货地址'), operate: 'LIKE', formatter:function (value, row, index) {
                            return row.province+row.city+row.district+row.address;
                        }},
                        {field: 'is_default', title: __('是否为默认地址'), formatter: Table.api.formatter.status, searchList: {'1': '是', '0': '否'}},
                       {field: 'add_time', title: __('Jointime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                   ]
               ]
           });
           // 为表格绑定事件
           Table.api.bindevent(table);
       },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});